﻿using System;
using System.Collections.Generic;
using System.Text;

namespace NETXPF.Library
{
    /// <summary>
    /// An extremely useful class for interning strings without the limitations of String.Intern.
    /// Refer to http://www.informit.com/guides/content.aspx?g=dotnet&seqNum=711
    /// </summary>
    public class InternTable
    {
        private Dictionary<string, string> stable = new Dictionary<string, string>();

        /// <summary>
        /// Returns the interned string
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public string Add(string s)
        {
            string sRet;
            if (!stable.TryGetValue(s, out sRet))
            {
                stable.Add(s, s);
                sRet = s;
            }
            return sRet;
        }

        public bool Remove(string s)
        {
            return stable.Remove(s);
        }

        public bool Contains(string s)
        {
            return stable.ContainsKey(s);
        }
    }
}
